package com.haoan.index.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.haoan.common.annotation.Anonymous;
import com.haoan.index.domain.Food;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haoan.common.annotation.Log;
import com.haoan.common.core.controller.BaseController;
import com.haoan.common.core.domain.AjaxResult;
import com.haoan.common.enums.BusinessType;
import com.haoan.index.domain.Announcement;
import com.haoan.index.service.AnnouncementService;
import com.haoan.common.utils.poi.ExcelUtil;
import com.haoan.common.core.page.TableDataInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 公告/新闻资讯Controller
 *
 * @author autoGen
 * @date 2025-01-03
 */
@Api(value="公告-新闻资讯",tags = "公告-新闻资讯")
@RestController
@RequestMapping("/index/announcement")
public class AnnouncementController extends BaseController
{
    @Resource
    private AnnouncementService announcementService;

    /**
     * 点赞
     */
    @ApiOperation("点赞")
    @Anonymous
    @GetMapping(value = "thumbsUp/{id}")
    public AjaxResult thumbsUp(@PathVariable("id") Long id)
    {
        Announcement announcement = announcementService.selectAnnouncementById(id);
        announcement.setLikes(announcement.getLikes()+1L);
        announcementService.updateAnnouncement(announcement);
        return success(announcement);
    }

    /**
     * 查询公告/新闻资讯列表
     */
    @ApiOperation("查询公告/新闻资讯列表")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(Announcement announcement)
    {
        startPage();
        List<Announcement> list = announcementService.selectAnnouncementList(announcement);
        return getDataTable(list);
    }

    /**
     * 导出公告/新闻资讯列表
     */
    @ApiOperation("导出公告/新闻资讯列表")
    @PreAuthorize("@ss.hasPermi('index:announcement:export')")
    @Log(title = "公告/新闻资讯", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Announcement announcement)
    {
        List<Announcement> list = announcementService.selectAnnouncementList(announcement);
        ExcelUtil<Announcement> util = new ExcelUtil<Announcement>(Announcement.class);
        util.exportExcel(response, list, "公告/新闻资讯数据");
    }

    /**
     * 获取公告/新闻资讯详细信息
     */
    @ApiOperation("获取公告/新闻资讯详细信息")
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(announcementService.selectAnnouncementById(id));
    }

    /**
     * 新增公告/新闻资讯
     */
    @ApiOperation("新增公告/新闻资讯")
    @PreAuthorize("@ss.hasPermi('index:announcement:add')")
    @Log(title = "公告/新闻资讯", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Announcement announcement)
    {
        return toAjax(announcementService.insertAnnouncement(announcement));
    }

    /**
     * 修改公告/新闻资讯
     */
    @ApiOperation("修改公告/新闻资讯")
    @PreAuthorize("@ss.hasPermi('index:announcement:edit')")
    @Log(title = "公告/新闻资讯", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Announcement announcement)
    {
        return toAjax(announcementService.updateAnnouncement(announcement));
    }

    /**
     * 删除公告/新闻资讯
     */
    @ApiOperation("删除公告/新闻资讯")
    @PreAuthorize("@ss.hasPermi('index:announcement:remove')")
    @Log(title = "公告/新闻资讯", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(announcementService.deleteAnnouncementByIds(ids));
    }
}
